package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Controller.AddNewCustomerController;
import Entity.Customer;
import Entity.Message;
import HandleDB.CustomerQuery;
import Communication.Client;
import View.*;

public class AddNewCustomerController {
	
	
	CustomerQuery customerModel;
	AddNewCustomerView customerView;
	Client client;



	
	public AddNewCustomerController(Client client, WorkerMainMenuController workerMainMenuController) {
			
		this.client = client;
		//customerModel = new CustomerQuery(client);
		customerView =  new AddNewCustomerView();
		customerView.showWindow();
	
			
		customerView.addNewCustomerListener(new CustomerInfoListener(workerMainMenuController));
	}
	
	
	class CustomerInfoListener implements ActionListener
	{
		WorkerMainMenuController workerMainController;
		
		
		public CustomerInfoListener(WorkerMainMenuController workerMainController){
			this.workerMainController = workerMainController;
			customerModel = new CustomerQuery();
			customerView.showWindow();
		}
		
		@Override
		public void actionPerformed(ActionEvent event) {
			
			
			if (event.getSource() == customerView.getAddBtn())
			{
				if (customerModel.isEmpty(customerView.getlastName()) || customerModel.isEmpty(customerView.getFirstName()) ||
						customerModel.isEmpty(customerView.getID()) || customerModel.isEmpty(customerView.getCity()) ||
						customerModel.isEmpty(customerView.getPhone()) || customerModel.isEmpty(customerView.getAge()))
					
					SystemController.displayErrorMessage("One or more fields are Empty");
				
				else
				{  customerView.showWindow();
					Customer Cst=new Customer();
					Cst.setCfirstName(customerView.getFirstName());
					Cst.setClastName(customerView.getlastName());
					Cst.setCustomerID(customerView.getID());
					Cst.setPhoneNumber(customerView.getPhone());
					Cst.setAge(Integer.parseInt(customerView.getAge()));
					Cst.setCity(customerView.getCity());
					
					try {
						customerModel.insert(Cst);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					SystemController.displayMessage("Customer Added successfully !");
					
					workerMainController.hideWindow();
					customerView.clearFields();
					Message msg = new Message();
					msg.setObj(Cst);
					msg.setMsg("AddNewCustomer");
					client.getData(msg);
				}
			}
			
			
			else if (event.getSource() == customerView.getBackBtn())
			{
				workerMainController.showWindow();
				customerView.hideWindow();
				customerView.clearFields();
			}
			
			
			
		}
	}
}



